package com.demo.controller;

import com.demo.common.Result;
import com.demo.service.IntelligentScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

/**
 * 智能排课控制器
 */
@RestController
@RequestMapping("/smart-schedule")
@CrossOrigin
public class SmartScheduleController {
    
    @Autowired
    @Qualifier("smartScheduleService")
    private IntelligentScheduleService smartScheduleService;
    
    /**
     * 执行智能排课（考虑教师课程对应关系）
     */
    @PostMapping("/execute/{semester}")
    public Result<String> executeSmartSchedule(@PathVariable String semester) {
        try {
            System.out.println("开始执行智能排课，学期：" + semester);
            
            boolean success = smartScheduleService.executeIntelligentSchedule(semester);
            
            if (success) {
                return Result.success("智能排课执行成功！学期：" + semester);
            } else {
                return Result.error("智能排课执行失败，请检查数据完整性");
            }
            
        } catch (Exception e) {
            System.err.println("智能排课执行异常：" + e.getMessage());
            e.printStackTrace();
            return Result.error("智能排课执行异常：" + e.getMessage());
        }
    }
    
    /**
     * 检查排课约束
     */
    @PostMapping("/check-constraints")
    public Result<String> checkConstraints(@RequestBody com.demo.pojo.entity.Schedule schedule) {
        try {
            boolean valid = smartScheduleService.checkConstraints(schedule);
            return Result.success("约束检查结果：" + (valid ? "通过" : "不通过"));
        } catch (Exception e) {
            return Result.error("约束检查失败：" + e.getMessage());
        }
    }
    
    /**
     * 计算排课质量
     */
    @PostMapping("/calculate-quality")
    public Result<String> calculateQuality(@RequestBody java.util.List<com.demo.pojo.entity.Schedule> schedules) {
        try {
            double quality = smartScheduleService.calculateScheduleQuality(schedules);
            return Result.success("排课质量评分：" + String.format("%.2f", quality) + "/1.0");
        } catch (Exception e) {
            return Result.error("质量计算失败：" + e.getMessage());
        }
    }
}
























